package com.example.fruit.adapter;

import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

import com.example.fruit.R;
import com.example.fruit.bean.Car;
import com.example.fruit.bean.Fruit;
import com.example.fruit.bean.Orders;
import com.example.fruit.bean.User;
import com.example.fruit.ui.activity.FruitOrderDetailActivity;
import com.example.fruit.ui.fragment.UserFragment;
import com.example.fruit.util.SPUtils;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
 * 退款适配器
 */

public class RefundAdapter extends RecyclerView.Adapter<RefundAdapter.ViewHolder> {
    private List<Orders> list =new ArrayList<>();
    private Context mActivity;
    private RequestOptions headerRO = new RequestOptions().circleCrop();//圆角变换
    private LinearLayout llEmpty;
    private RecyclerView rvOrderList;
    private ItemListener mItemListener;
    private String[] s=new String[]{"在线客服","服务热线"}; int j;
    public void setItemListener(ItemListener itemListener){
        this.mItemListener = itemListener;
    }
    public RefundAdapter(LinearLayout llEmpty, RecyclerView rvOrderList){
        this.llEmpty = llEmpty;
        this.rvOrderList =rvOrderList;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        mActivity = viewGroup.getContext();
        View view= LayoutInflater.from(mActivity).inflate(R.layout.item_rv_refund_list,viewGroup,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        Orders order = list.get(i);
        Fruit fruit = DataSupport.where("title = ?",order.getTitle()).findFirst(Fruit.class);
        //Issue issue=DataSupport.where("title",order.getTitle()).findFirst(Issue.class);
        User user = DataSupport.where("account = ? ", order.getAccount()).findFirst(User.class);
        if (order != null && user!=null && fruit != null) {
            viewHolder.number.setText(String.format("订单号：%s", order.getNumber()));
            viewHolder.j.setText(order.getIssuer());//order.getNumber
            viewHolder.title.setText(order.getTitle());
            Glide.with(mActivity)
                    .asBitmap()
                    .load(fruit.getImg())
                    .error(R.drawable.ic_error)
                    .skipMemoryCache(true)
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .into(viewHolder.img);
            viewHolder.tuikuan.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
                    builder.setIcon(R.drawable.a1);//设置图标
                    builder.setTitle("请选择退款渠道");//设置对话框的标题
                    builder.setSingleChoiceItems(s, 0, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            j = which;
                        }
                    });
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                   if (j == 0) {
                                        //在线客服
                                        try {
                                            Toast.makeText(mActivity,"请提供订单编号联系【"+s[j]+"】申请退款! 如无法联系，请添加好友后再试!", Toast.LENGTH_LONG).show();
                                            mActivity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("mqqapi://card/show_pslcard?src_type=internal&source=sharecard&version=1&uin=1632957243")));//跳转到QQ资料

                                        } catch (Exception e) {
                                            //异常处理
                                            Toast.makeText(mActivity,"请安装最新版QQ后使用此功能!", Toast.LENGTH_LONG).show();
                                        }
                                    }
                                    if (j == 1) {
                                        try {
                                            //呼叫
                                            Intent intent=new Intent(Intent.ACTION_DIAL);
                                            Uri data =Uri.parse("tel:"+"4007777777");
                                            intent.setData(data);
                                            mActivity.startActivity(intent);
                                            Toast.makeText(mActivity,"请提供订单编号呼叫【"+s[j]+"】申请退款!点击拨号键呼叫24小时免费服务热线", Toast.LENGTH_LONG).show();
                                        } catch (Exception e) {
                                            //异常处理
                                        }
                                    }
                                }
                            }
                    );
                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Toast.makeText(mActivity, "取消!", Toast.LENGTH_SHORT).show();
                                }
                            }
                    );
                    AlertDialog a = builder.create();
                    a.show();
                }});
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mActivity, FruitOrderDetailActivity.class);
                    Fruit fruit = DataSupport.where("title = ?",order.getTitle()).findFirst(Fruit.class);
                    intent.putExtra("fruit",fruit);
                    mActivity.startActivity(intent);
                }
            });
        }

    }
    public void addItem(List<Orders> listAdd) {
        //如果是加载第一页，需要先清空数据列表
        this.list.clear();
        if (listAdd!=null){
            //添加数据
            this.list.addAll(listAdd);
        }
        //通知RecyclerView进行改变--整体
        notifyDataSetChanged();
    }
    @Override
    public int getItemCount() {
        return list.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder {
        private Button tuikuan;
        private TextView j;
        private TextView number;
        private TextView title;
        private ImageView img;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            number = itemView.findViewById(R.id.number);
            title = itemView.findViewById(R.id.title);
            j=itemView.findViewById(R.id.j);
            tuikuan=itemView.findViewById(R.id.tuikuan);
            img = itemView.findViewById(R.id.img);
        }
    }

    public interface ItemListener{
        void ItemClick(Orders order);
    }

}
